package com.zzzy.common.util;

import com.zzzy.common.exception.WarningException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * Description: 断言工具类
 *
 * @author fc
 * @date 2023/3/14
 */
public class AssertUtil {
    /**
     *  如果参数不为true抛出异常
     **/
    public static void isTrue(boolean b, String message){
        if (!b){
            throw new WarningException(message);
        }
    }

    public static <T> void isNull(T object, String message){
        isTrue(Objects.isNull(object), message);
    }

    public static <T> void nonNull(T object, String message){
        isTrue(Objects.nonNull(object), message);
    }

    public static void isBlank(String str, String message){
        isTrue(StringUtils.isBlank(str), message);
    }

    public static void isNotBlank(String str, String message){
        isTrue(StringUtils.isNotBlank(str), message);
    }

    public static <T> void isEquals(T firstObj, T secondObj, String message){
        isTrue(Objects.equals(firstObj, secondObj), message);
    }

    public static <T> void isNotEquals(T firstObj, T secondObj, String message){
        isTrue(!Objects.equals(firstObj, secondObj), message);
    }

    public static void isEmpty(Collection<?> collection, String message){
        isTrue(CollectionUtils.isEmpty(collection), message);
    }

    public static void isEmpty(Map<?, ?> map, String message){
        isTrue(CollectionUtils.isEmpty(map), message);
    }

    public static void isNotEmpty(Map<?, ?> map, String message){
        isTrue(!CollectionUtils.isEmpty(map), message);
    }

    public static void isNotEmpty(Collection<?> collection, String message){
        isTrue(!CollectionUtils.isEmpty(collection), message);
    }

}
